<?php

namespace app\admin\controller\cuiban;

use addons\fastchat\library\Chat;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 补偿催办
 *
 * @icon fa fa-circle-o
 */
class Buchang extends Backend
{

    /**
     * Buchang模型对象
     * @var \app\admin\model\cuiban\Buchang
     */
    protected $model = null;
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\cuiban\Buchang;
        $admin = $this->auth->getUserInfo();

        $this->assign('admin',$admin);
        $rule = Db::name('auth_group_access')->alias('a')->join('auth_group b','a.group_id = b.id')->where('a.uid',$admin['id'])->field('b.rules,a.group_id')->find();
        $rules = $rule['rules'];
        if($rules == '*'){
            $rules = Db::name('auth_rule')->column('id');
        }else{
            $rules = explode(',',$rule['rules']);
        }
        $this->rule = $rules;
        $this->assignconfig('rule', $rules);
        $this->assign('rules', $rules);

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['username']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function search()
    {
        return $this->view->fetch();
    }

    /**
     * 查询是否存在记录
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chaxun(){
        $params = $this->request->post('');
        $where = [];
        if($params['car_num']){
            $where['car_num'] = $params['car_num'];
        }
        if($params['phone']){
            $where['phone'] = $params['phone'];
        }
        if(!$where) $this->error('至少填写一项');
        $info = Db::name('buchang_cuiban')->where($where)->where('deletetime',)->find();
        if(!$info){
            $admin = $this->auth->getUserInfo();
            $data['car_num'] = $params['car_num'];
            $data['phone'] = $params['phone'];
            $data['admin_id'] = $admin['id'];
            $data['createtime'] = time();
            $data['updatetime'] = time();
            $id = Db::name('buchang_cuiban')->insertGetId($data);
        }else{
            $id = $info['id'];
        }
        $this->success('','',['id'=>$id]);
    }

    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function add($ids = null)
    {
        if (false === $this->request->isPost()) {
            $params = $this->request->get('');
            if(isset($params['id']) && $params['id']){
                $id = $params['id'];
            }else{
                $id = $ids;
            }
            if(!$id) $this->error('参数错误');
            $res = Db::name('buchang_cuiban')->where('id',$id)->find();
            if($res['car_num']){
                $row = Db::name('buchang_cuiban_list')->alias('a')->join('buchang_cuiban b','a.bid = b.id')->where('b.car_num',$res['car_num'])->where('a.deletetime',)->where('b.deletetime',)->order('a.id asc')->field('a.*,b.car_num,b.phone')->select();
            }else{
                $row = Db::name('buchang_cuiban_list')->where('bid',$id)->where('deletetime',)->order('id asc')->select();
            }

            $this->view->assign('res',$res);
            $this->view->assign('row',$row);
            return $this->view->fetch();
        }
    }

    /**
     * 保存客服记录
     * @return void
     */
    public function save_list(){
        $params = $this->request->post('');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $result = false;
        $id = 0;
        Db::startTrans();
        try {
            if($params['id']){
                $data['type'] = $params['type'];
                $data['date'] = $params['date'];
                $data['content'] = $params['content'];
                $data['updatetime'] = time();
                $result = Db::name('buchang_cuiban_list')->where('id',$params['id'])->update($data);
            }else{
                $data['type'] = $params['type'];
                $data['date'] = $params['date'];
                $data['content'] = $params['content'];
                $data['user_id'] = $params['userid'];
                $data['user_name'] = $params['name'];
                $data['bid'] = $params['bid'];
                $data['createtime'] = time();
                $data['updatetime'] = time();
                $result = Db::name('buchang_cuiban_list')->insertGetId($data);
                $id = $result;
            }
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success('','',['id'=>$id]);
    }
}
